package com.chungvb.utility;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import org.xmlpull.v1.XmlSerializer;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Environment;
import android.util.Log;
import android.util.Xml;

import com.chungbv.table.Songs;
import com.karaokevietnam7so.R;

public class XmlFileCreator
{
	private ArrayList<Songs> list;
	private File filename;
	private Context context;

	public XmlFileCreator(String name, ArrayList<Songs> list, Context context) {
		this.list = list;
		this.filename = new File(Environment.getExternalStorageDirectory() + name);
		this.context = context;
		checkFileNameAndCreate();
	}

	public void checkFileNameAndCreate()
	{
		// file existing.
		if (filename.exists()) {
			showMessageBox();
		}
		else {
			createXMLFile();
		}
	}

	public void createXMLFile()
	{
		try {
			filename.createNewFile();
		}
		catch (IOException e) {
			Log.e("IOException", "exception in createNewFile() method");
		}

		// we have to bind the new file with a FileOutputStream
		FileOutputStream fileos = null;
		
		try {
			fileos = new FileOutputStream(filename);
		}
		catch (FileNotFoundException e) {
			Log.e("FileNotFoundException", "can't create FileOutputStream");
		}

		// we create a XmlSerializer in order to write xml data
		XmlSerializer serializer = Xml.newSerializer();
		
		try {
			// we set the FileOutputStream as output for the serializer, using
			// UTF-8 encoding
			serializer.setOutput(fileos, "UTF-8");

			// Write <?xml declaration with encoding (if encoding not null) and
			// standalone flag (if standalone not null)
			serializer.startDocument(null, Boolean.valueOf(true));

			// set indentation option
			serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);

			// start a tag called "root"
			serializer.startTag(null, "favorite");

			for (int i = 0; i < list.size(); i++) {
				serializer.startTag(null, "item");

				// ID
				serializer.startTag(null, "id");
				serializer.text(list.get(i).getSongId());
				serializer.endTag(null, "id");

				// TIME
				serializer.startTag(null, "time");
				serializer.text(list.get(i).getLastUpdate());
				serializer.endTag(null, "time");

				serializer.endTag(null, "item");
			}

			serializer.endTag(null, "favorite");
			serializer.endDocument();

			// write xml data into the FileOutputStream
			serializer.flush();

			// finally we close the file stream
			fileos.close();	
		}
		catch (Exception e) {
			Log.e("Exception", "error occurred while creating xml file");
		}	
		
		Utility.ShowMessageBox(context, context.getResources().getString(R.string.favorite_savesong));	
	}

	public void showMessageBox()
	{
		DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener()
		{
			@Override
			public void onClick(DialogInterface dialog, int which)
			{
				switch (which)
				{
					case DialogInterface.BUTTON_POSITIVE:
						// Yes button clicked
						createXMLFile();
						break;

					case DialogInterface.BUTTON_NEGATIVE:
						// No button clicked

						break;
				}
			}
		};

		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setMessage(context.getResources().getString(R.string.mess_header))
				.setPositiveButton(context.getResources().getString(R.string.mess_yes), dialogClickListener)
				.setNegativeButton(context.getResources().getString(R.string.mess_no), dialogClickListener).show();

	}	
}
